(function($) {
	/***************************************************************************
	 * JQuery 明细视图插件12.0
	 * 
	 * Author: 姜志光 Date: 2014年1月8日
	 **************************************************************************/

	ListView = function(rootElement, userSettings) {

		var instance = this;

		/***********************************************************************
		 * 数据来源表单
		 **********************************************************************/
		this.fromForm = userSettings.fromForm;

		/***********************************************************************
		 * 视图主键配置
		 **********************************************************************/
		this.pfields = userSettings.pfields;

		this.viewConfig = userSettings;

		/***********************************************************************
		 * 视图当前选中行
		 **********************************************************************/
		this.selectRow;

		this.viewOperate = null;

		/***********************************************************************
		 * 视图初始化
		 **********************************************************************/
		this.init = function() {

			instance.datas	=	instance.viewConfig.datas;
			instance.viewOperate = new ViewOperate(instance,
					instance.viewConfig.operateConfig, $("#operateDiv"));
			/*******************************************************************
			 * 视图操作初始化
			 ******************************************************************/
			instance.viewOperate.operateDraw();

			/*******************************************************************
			 * 列表初始化
			 ******************************************************************/
			var formatters = new Array();
			var table = $("#listTable");
			var firstTR = $("<tr/>");

			var listXML = instance.viewConfig.listXML;
			if (undefined != listXML) {
				$("#pageTD").attr("colspan", listXML.length);
				for ( var x = 0; x < listXML.length; x++) {

					var list = listXML[x];

					var th = $("<th/>");
					th.html(list["title"]);
					th.attr("rowspan", list.rowspan);
					th.attr("colspan", list.colspan);
					if(list.width > 0 ){
						th.width(list.width);	
					}
					
					if (undefined != list["formatter"]) {
						formatters[x] = list["formatter"];
					} else {
						formatters[x] = list["field"];
					}
					firstTR.append(th);
				}
				table.append(firstTR);
			}

			if (undefined != instance.datas) {
				for ( var x = 0; x < instance.datas.length; x++) {
					var data = instance.datas[x];
					var tr = $("<tr/>");
					for ( var y = 0; y < listXML.length; y++) {
						var td = $("<td/>");
						var id = formatters[y];
						var content = "";
						if (typeof (id) == "function") {
							content = id(data, "");
						} else {
							content = data[id];
						}
						td.html(content);
						tr.append(td);
					}
					table.append(tr);
				}
			}

		};

		/***********************************************************************
		 * 数据添加操作
		 **********************************************************************/
		this.onAdd = function() {
			if ('' != fromForm) {
				window.location.href	=	application+'/goForm?vf_guid='+fromForm+"&fromView="+instance.viewConfig.vv_guid;
			}
		};

		/***********************************************************************
		 * 数据编辑操作
		 **********************************************************************/
		this.onEdit = function(obj) {
			
			if(undefined == obj){
				return;
			}
			if($("#detailForm").length == 0){
				alert("未配置数据来源表单");
				return;
			}
			
			if(undefined == instance.pfields || 0 == instance.pfields.length){
				alert("视图未配置主键，无法跳转到表单");
				return;
			}
			var rowIndex	=	obj.parentNode.parentNode.rowIndex;
			var row	=	instance.datas[rowIndex-1];
			$("#detailDiv").html("");
			for(var x=0; x<instance.pfields.length;x++){
				$("#detailDiv").append(
					"<input type='hidden' name='viewPValues."+instance.pfields[x]+"' value='"+row[pfields[x]]+"'/>");
			}
			$("#detailForm").submit();
		};

		/***********************************************************************
		 * 数据删除操作
		 **********************************************************************/
		this.onDelete = function(obj) {
			
			if(undefined == obj){
				return;
			}
			if(!confirm("您确定要删除该记录")){
				return;
			}
			if($("#detailForm").length == 0){
				alert("未配置数据来源表单");
				return;
			}
			
			var rowIndex	=	obj.parentNode.parentNode.rowIndex;
			var row	=	datas[rowIndex-1];
			$("#deleteDiv").html("");
			for(var x=0; x<pfields.length;x++){
				$("#deleteDiv").append(
					"<input type='hidden' name='pvalues."+pfields[x]+"' value='"+row[pfields[x]]+"'/>");
			}
			$('#deleteForm').ajaxSubmit(function(data) {
				if("成功删除表单数据" == data){
					instance.refreshView();
				}else{
					alert(data);
				}
			});
		};

		/***********************************************************************
		 * 数据批量删除操作
		 **********************************************************************/
		this.onBatDelete = function() {
			var rows	=	instance.getSelectedRows();
			if (undefined == rows || rows.length == 0) {
				alert("未选中行");
				return;
			}

			if (!confirm("您确定要删除选中记录")) {
				return;
			}
			if ($("#batDeleteForm").length == 0) {
				alert("未配置数据来源表单");
				return;
			}

			var array	=	new Array();
			for(var index=0; index<rows.length; index++){
				var row	=	rows[index];
				for ( var x = 0; x < instance.pfields.length; x++) {
					var jobject	=	new Object();
					jobject[instance.pfields[x]]	=	row[instance.pfields[x]];
					array[array.length]	=	jobject;
				}
			}
			
			$("#batDeleteContents").val($.toJSON(array));
			$('#batDeleteForm').ajaxSubmit(function(data) {
				if ("成功删除记录" == data) {
					alert(data);
					searchForm();
				} else {
					alert(data);
				}
			});
		};

		/***********************************************************************
		 * 数据情况操作
		 **********************************************************************/
		this.onClear = function() {

		};

		/***********************************************************************
		 * 页面跳转操作
		 **********************************************************************/
		this.onForward = function() {

		};

		/***********************************************************************
		 * Excel导入操作
		 **********************************************************************/
		this.onExcelImport = function() {

		};

		/***********************************************************************
		 * Excel导出操作
		 **********************************************************************/
		this.onExcelExport = function() {

		};

		/***********************************************************************
		 * 视图刷新
		 **********************************************************************/
		this.refreshView = function() {
			if("undefined" != typeof(searchForm)){
				searchForm();
			}else{
				window.location.reload();
			}
		};

		this.onSearch = function() {
			
		};

		/***********************************************************************
		 * 获取视图多选行
		 **********************************************************************/
		this.getSelectedRows = function() {
			var checks = $(".dataSelectCheck");
			var alls = instance.viewConfig.datas;
			var rows = new Array();
			for ( var x = 0; x < checks.length; x++) {
				var check = $(checks[x]);
				var checked = check.attr("checked");
				if (true == checked || "checked" == checked) {
					rows[rows.length] = alls[x];
				}
			}
			return rows;
		};
		
		/***********************************************************************
		 * 获取视图选中行
		 **********************************************************************/
		this.getSelectedRow	=	function(){
			return ;
		};
	};

	$.fn.listView = function(action) {
		var i, ilen, args, output, userSettings, $this = this.first(), instance = $this
				.data('ListView');
		if (typeof action !== 'string') {
			userSettings = action || {};
			if (instance) {
				instance.updateSettings(userSettings);
			} else {
				instance = new ListView($this, userSettings);
				$this.data('ListView', instance);
				instance.init();
			}
			return $this;
		} else {
			args = [];
			if (arguments.length > 1) {
				for (i = 1, ilen = arguments.length; i < ilen; i++) {
					args.push(arguments[i]);
				}
			}

			if (instance) {
				if (typeof instance[action] !== 'undefined') {
					output = instance[action].apply(instance, args);
				} else {
					throw new Error('ListView do not provide action: ' + action);
				}
			}
			return output;
		}
	}
})(jQuery);

function showDataSelect(value, rec) {
	return "<input type='checkbox' class='dataSelectCheck' onclick='onDataSelectCheckClick()'/>";
}

function onDataSelectCheckClick(){
	listView.viewOperate.refreshOperates();
}

function showDataEdit(value,rec){
	return "<a href='#' onclick='onEditData(this)'><font color=red>修改</font></a>";
}
function showDataDelete(value,rec){
	return "<a href='#' onclick='onDeleteData(this)'><font color=red>删除</font></a>";
}
function showDataEditDelete(value,rec){
	return "<a href='#' onclick='onEditData(this)'>修改</a>&nbsp;<a href='#' onclick='onDeleteData(this)'>删除</a>";
}
function showDictMean(value,rec){
	return "";
}

function onEditData(obj) {
	listView.onEdit(obj);
}

function onDeleteData(obj) {
	listView.onDelete(obj);
}

loadResource(application + "/resource/jquery/jquery.form.js", "js");
loadResource(application + "/resource/lib/jquery.json-2.4.js", "js");

loadResource(application + "/resource/platform/skins/ace/js/designForm.js",
		"js");
loadResource(application + "/resource/platform/skins/ace/js/viewOperate.js",
		"js");

loadResource(application + "/resource/platform/skins/ace/js/formInput.js", "js");

loadResource(application + "/resource/jquery/jquery.autocomplete.js", "js");

loadResource(application + "/resource/platform/skins/ace/css/listView.css",
"css");
loadResource(application + "/resource/platform/skins/ace/css/bootstrap.min.css",
		"css");
loadResource(application + "/resource/platform/skins/ace/css/ace.min.css",
		"css");
loadResource(application + "/resource/platform/skins/ace/css/font-awesome.min.css",
		"css");
function loadResource(filename, filetype) {
	if (filetype == "js") { // if filename is a external JavaScript file
		var sc = "<script src='" + filename + "'></script>";
		document.write(sc);
	} else if (filetype == "css") { // if filename is an external CSS
		var sc = "<link rel='stylesheet' type='text/css' " + "href='"
				+ filename + "' />";
		document.write(sc);
	}
	if (typeof fileref != "undefined")
		document.getElementsByTagName("head")[0].appendChild(fileref)
}